﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.BaseObject;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Contract
{
    /// <summary>
    /// ContractInfo 的摘要说明
    /// </summary>
    public class ContractInfo : IHttpHandler
    {

        private string GetPageUrl(Utils.ContractTreeModule treeModule)
        {
            string pageUrl = "";
            switch (treeModule)
            {
                case Utils.ContractTreeModule.Contract:
                    pageUrl = "BD_Contract_List.aspx";
                    break;
                case Utils.ContractTreeModule.Change:
                    pageUrl = "BD_ContractChange_List.aspx";
                    break;
                case Utils.ContractTreeModule.Payment:
                    pageUrl = "BD_Payment_List.aspx";
                    break;
                case Utils.ContractTreeModule.InitConfirm:
                    pageUrl = "ContractConfirmList.aspx";
                    break;
                case Utils.ContractTreeModule.DocReceive:
                    pageUrl = Globals.Home + "Module/Audit/Bu_DocReceive_List.aspx";
                    break;
                default:
                    break;
            }
            return pageUrl;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string parentId = context.Request.Params["ID"];
            Honestysoft.Financing.BusinessLogic.Contract contractBLL = new BusinessLogic.Contract("");
            Utils.ContractTreeModule treeModule = (Utils.ContractTreeModule)int.Parse(context.Request.Params[Utils.ParamName_ContractTreeModule]);
            //List<Honestysoft.Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode> lsNode = new List<Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode>();
            string responseData = "";
            //Baseframework.WebUI.JQueryEasyUI.Tree.AsynTreeNode node = null;
            List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsProjectCategoryDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ProjectCategory");           
            List<Honestysoft.DictionaryHelper.Sys_Dictionary> lsContractCategoryDict = Honestysoft.DictionaryHelper.DictionaryInstance.GetDictionaryList("ContractCategory");
            if (lsProjectCategoryDict != null && lsProjectCategoryDict.Count > 0)
            {
                lsProjectCategoryDict = lsProjectCategoryDict.FindAll((Honestysoft.DictionaryHelper.Sys_Dictionary find) => find.IsValid == true);
            }
            if (lsContractCategoryDict != null && lsContractCategoryDict.Count > 0)
            {
                lsContractCategoryDict = lsContractCategoryDict.FindAll((Honestysoft.DictionaryHelper.Sys_Dictionary find) => find.IsValid == true);
            }
            string pageUrl = "";

            List<zTreeUtil.zTreeNode> lsZNodes = new List<zTreeUtil.zTreeNode>();
            zTreeUtil.zTreeNode zNode = null;

            if (parentId == null) //根未加载
            {
                zNode = new zTreeUtil.zTreeNode();
                zNode.id = "0";
                if (treeModule == Utils.ContractTreeModule.Contract
                    || treeModule == Utils.ContractTreeModule.InitConfirm)
                {
                    zNode.name = "项目树";
                }
                else
                {
                    zNode.name = "合同树";
                }
                zNode.isParent = (lsProjectCategoryDict == null ? false : lsProjectCategoryDict.Count > 0);
                zNode.nodeType = ((int)Utils.ContractTreeNodeType.Root).ToString();
                zNode.url = GetPageUrl(treeModule) + "?ID=0";
                zNode.target = "ifrmInfo";
                zNode.icon = "../../images/computer.png";
                lsZNodes.Add(zNode);
            }
            else
            {
                Utils.ContractTreeNodeType nodeType = (Utils.ContractTreeNodeType)int.Parse(context.Request.Params[Utils.ParamName_ContractNodeType]);
                if (nodeType == Utils.ContractTreeNodeType.Root)
                {
                    DataTable dt = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectCategoryCount(null);
                    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsProjectCategoryDict)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/company.gif";
                        zNode.id = item.DictValue;
                        zNode.name = item.DictName;
                        zNode.nodeType = ((int)Utils.ContractTreeNodeType.ProjectCategory).ToString();
                        zNode.target = "ifrmInfo";
                        zNode.url = GetPageUrl(treeModule) + "?ID=" + zNode.id;
                        if (dt != null)
                        {
                            DataRow[] rows = dt.Select("ProjectCategory='" + zNode.id + "'");
                            if (rows != null && rows.Length == 1)
                            {
                                zNode.isParent = Convert.ToInt32(rows[0]["ChildNodes"]) > 0;
                            }
                            else
                            {
                                zNode.isParent = false;
                            }
                        }
                        else
                        {
                            zNode.isParent = false;
                        }
                        lsZNodes.Add(zNode);
                    }
                }
                else if (nodeType == Utils.ContractTreeNodeType.ProjectCategory)//项目类别
                {
                    DataTable dtProject = new Honestysoft.Financing.BusinessLogic.Project("").GetProjectsByCategory(parentId, null);
                    foreach (DataRow dr in dtProject.Rows)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/database_gear.png";
                        zNode.id = dr["ID"].ToString();
                        zNode.isParent = (lsContractCategoryDict == null ? false : lsContractCategoryDict.Count > 0);
                        zNode.name = dr["SN"].ToString() + " " + dr["Name"].ToString();
                        zNode.nodeType = ((int)Utils.ContractTreeNodeType.Project).ToString();
                        zNode.target = "ifrmInfo";
                        zNode.url = GetPageUrl(treeModule) + "?ID=" +  zNode.id;
                        lsZNodes.Add(zNode);
                    }
                }
                else if (nodeType == Utils.ContractTreeNodeType.Project)
                {
                    DataTable dtContractCategoryCount= null;
                    //if (treeModule == Utils.ContractTreeModule.InitConfirm)
                    //{
                        dtContractCategoryCount = contractBLL.GetContractCategoryCount(parentId, null);
                    //}
                    //else
                    //{
                    //    dtContractCategoryCount = contractBLL.GetContractCategoryCount(parentId, true);
                    //}
                    foreach (Honestysoft.DictionaryHelper.Sys_Dictionary item in lsContractCategoryDict)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/book_red.png";
                        zNode.id = item.DictValue;
                        if (treeModule == Utils.ContractTreeModule.Contract)
                        {
                            zNode.isParent = false;
                        }
                        else
                        {
                            //得到合同个数
                            if (dtContractCategoryCount != null)
                            {
                                zNode.isParent = dtContractCategoryCount.Select("ContractCategory='" + item.DictValue + "'").Length > 0;
                            }
                            else
                            {
                                zNode.isParent = false;
                            }
                        }
                        zNode.name = item.DictName;
                        zNode.nodeType = ((int)Utils.ContractTreeNodeType.ContractCategory).ToString();
                        zNode.target = "ifrmInfo";
                        zNode.url = GetPageUrl(treeModule) + "?ID=" + zNode.id + "&ParentID=" + parentId;
                        zNode.ext1 = parentId;
                        lsZNodes.Add(zNode);
                    }
                }
                else if (nodeType == Utils.ContractTreeNodeType.ContractCategory) //合同类别
                {                    
                    DataTable dtContracts = null;
                    Honestysoft.Financing.BusinessLogic.Contract bll = new BusinessLogic.Contract("");
                    //if (treeModule != Utils.ContractTreeModule.InitConfirm)
                    //{
                    //    dtContracts = bll.GetContracts(context.Request.Params["ext1"], parentId, true);
                    //}
                    //else
                    //{
                        dtContracts = bll.GetContracts(context.Request.Params["ext1"], parentId, null);
                    //}
                    string code="";
                    foreach (DataRow dr in dtContracts.Rows)
                    {
                        zNode = new zTreeUtil.zTreeNode();
                        zNode.icon = "../../images/page_white_swoosh.png";
                        zNode.id = dr["ID"].ToString();
                        zNode.isParent = false;
                        code = dr["Code"].ToString();
                        zNode.name = code.Substring(code.IndexOf("C")) + " " + dr["Name"].ToString().Replace("\n","");

                        if (treeModule == Utils.ContractTreeModule.InitConfirm)
                        {
                            zNode.url = "ContractConfirmInfo.aspx?ID=" + zNode.id;
                        }
                        else
                        {
                            zNode.url = GetPageUrl(treeModule) + "?ID=" + zNode.id;
                        }
                        zNode.target = "ifrmInfo";
                        zNode.nodeType = ((int)Utils.ContractTreeNodeType.Contract).ToString();
                        lsZNodes.Add(zNode);
                    }
                }
                else if (nodeType == Utils.ContractTreeNodeType.Contract) //合同
                {
                }
            }
            responseData = zTreeUtil.GetJsonString(lsZNodes);
            context.Response.Write(responseData);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}